package com.hjxr.quota.theme.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.hjxr.quota.theme.domain.QuotaThemeSet;
import com.hjxr.quota.theme.service.IQuotaThemeSetService;
import com.hjxr.quota.valid.AddGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/set")
@Api(tags = "主题集接口")
public class QuotaThemeSetController extends ApiController {

    @Resource
    private IQuotaThemeSetService quotaThemeSetService;

    /**
     * 获取set树形数据
     */
    @GetMapping("/getThemeSetTree/{zoneId}")
    @ApiOperation("主题集树形数据")
    public R<List<QuotaThemeSet>> getThemeSetTree(@PathVariable("zoneId") Integer zoneId) {
        if (zoneId == null) {
            return failed("请选择主题域");
        }
        List<QuotaThemeSet> themeSetTree = quotaThemeSetService.getThemeSetTree(zoneId);
        return success(themeSetTree);
    }

    @ApiOperation("根据父id查询主题集")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "parentId", value = "父节点id", defaultValue = "0", required = true),
            @ApiImplicitParam(dataType = "Integer", name = "zoneId", value = "主题域id", required = true)
    })
    @GetMapping("/getSetList")
    public R<List<QuotaThemeSet>> getSetList(@RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
                                             @RequestParam("zoneId") Integer zoneId) {
        List<QuotaThemeSet> themeSetTree = quotaThemeSetService.getSetList(zoneId, parentId);
        return success(themeSetTree);
    }

    /**
     * 获取set详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("主题集详情")
    public R<QuotaThemeSet> getInfo(@PathVariable("id") Integer id) {
        return success(quotaThemeSetService.getInfo(id));
    }

    /**
     * 新增set
     */
    @PostMapping
    @ApiOperation("新增主题集")
    public R<Boolean> add(@Validated({AddGroup.class}) @RequestBody QuotaThemeSet quotaThemeSet) {
        if (quotaThemeSet.getParentId() == null) {
            quotaThemeSet.setParentId(0);
        }
        quotaThemeSet.setCreateDate(new Date());
        return success(quotaThemeSetService.saveThemeSet(quotaThemeSet));
    }

    /**
     * 修改set
     */
    @PutMapping
    @ApiOperation("更新主题集")
    public R<Boolean> edit(@RequestBody QuotaThemeSet quotaThemeSet) {
        return success(quotaThemeSetService.updateThemeSet(quotaThemeSet));
    }

    /**
     * 删除set
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除主题集")
    public R<Boolean> remove(@PathVariable Integer[] ids) {
        if (null == ids || ids.length <= 0) {
            return failed("无效目标");
        }
        return success(quotaThemeSetService.removeThemeSetByIds(Arrays.asList(ids)));
    }

    @PostMapping("/getThemeSetByQuotaId")
    @ApiOperation(value = "根据指标id查询指标所属主题集名称")
    public R<List<QuotaThemeSet>> getThemeSetByQuotaId(@RequestBody List<Integer> quotaIds) {
        if (null == quotaIds || quotaIds.size() <= 0) {
            return failed("参数非法，请联系管理员");
        }
        return success(quotaThemeSetService.getThemeSetByQuotaId(quotaIds));
    }
}
